package com.example.voucher.contract.spi;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.example.voucher.contract.VoucherItemCapable;
import com.example.voucher.entity.InvoiceVerifyDetail;
import com.example.voucher.entity.PurchaseInvoice;
import com.example.voucher.enums.VoucherScenarioEnum;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
public class VoucherItemCapablePurchaseInvoiceIvdAdapter implements VoucherItemCapable {

    private static final Logger LOGGER = LoggerFactory.getLogger(VoucherItemCapablePurchaseInvoiceIvdAdapter.class);

    private InvoiceVerifyDetail ivd;
    private PurchaseInvoice invoice;
    private Map<String, String> itIcMapping;

    @Override
    public String getOrgTitleCode() {
        return invoice.getLeCode();
    }

    @Override
    public String getOrgTitleName() {
        return invoice.getLeName();
    }
    
    @Override
    public String getOrgTitleFullname() {
        return invoice.getLeFullname();
    }

    @Override
    public String getOppoOrgTitleCode() {
        return ivd.getTsTrCode();
    }

    @Override
    public String getOppoOrgTitleName() {
        return ivd.getTsTrName();
    }

    @Override
    public String getOppoOrgTitleFullname() {
        return ivd.getTsTrFullname();
    }
    
    @Override
    public String getInvoiceCode() {
        return ivd.getInvoiceCode();
    }

    @Override
    public String getInvoiceNo() {
        return ivd.getInvoiceNo();
    }

    @Override
    public Date getInvoiceDate() {
        return invoice.getBillingDate();
    }

    @Override
    public Long getInvoiceId() {
        return ivd.getInvoiceId();
    }

    @Override
    public Integer getInvoiceSn() {
        if (null != ivd.getInvoiceLineNum()) {
            return ivd.getInvoiceLineNum().intValue();
        }
        return null;
    }

    @Override
    public Long getInvoiceSnId() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public BigDecimal getWeight() {
        return ivd.getVerifyQty();
    }

    @Override
    public BigDecimal getPreTaxAmount() {
        return ivd.getVerifyPretaxAmt();
    }

    @Override
    public BigDecimal getTaxAmount() {
        return ivd.getVerifyAmt().subtract(ivd.getVerifyPretaxAmt());
    }

    @Override
    public BigDecimal getPriceTaxAmount() {
        return ivd.getVerifyAmt();
    }

    @Override
    public String getOrderCode() {
        if (StringUtils.isBlank(ivd.getPoExternalNo())) {
            return ivd.getPoCode();
        }
        return ivd.getPoExternalNo();
    }

    @Override
    public Date getOrderDate() {
        return ivd.getPoDate();
    }

    @Override
    public String getRcCode() {
        return ivd.getRcCode();
    }

    @Override
    public String getRcName() {
        return ivd.getRcName();
    }

    @Override
    public String getProductLine() {
        return ivd.getProductLine();
    }

    @Override
    public String getScenario() {
        // xssy && 货款，返回VoucherScenarioEnum.PurchaseInvoice_GOODS.getCode()
        // xssy && 费用，返回VoucherScenarioEnum.PurchaseInvoice_EXPENSE.getCode()
        // ygzy 返回VoucherScenarioEnum.PurchaseInvoice_GOODS.getCode()
        String scenario = invoice.getInvoiceType() == 1 ? VoucherScenarioEnum.PurchaseInvoice_GOODS.getCode()
            : VoucherScenarioEnum.PurchaseInvoice_EXPENSE.getCode();
        return scenario;
    }

    @Override
    public String getSiteCode() {
        return ivd.getSiteCode();
    }

    @Override
    public String getSiteName() {
        return ivd.getSiteName();
    }

    @Override
    public String getSiteFullname() {
        return ivd.getSiteFullname();
    }

    @Override
    public Date getScanDate() {
        return invoice.getScanDate();
    }

    @Override
    public String getUnit() {
        return ivd.getUnit();
    }

    @Override
    public String getQtyUnit() {
        return ivd.getQuantity().stripTrailingZeros().toPlainString() + ivd.getUnit();
    }

    @Override
    public String getItName() {
        return ivd.getItName();
    }
    
    @Override
    public String getIcName() {
        if (MapUtils.isEmpty(itIcMapping) || !itIcMapping.containsKey(ivd.getItName())) {
            LOGGER.warn("找不到品类名，入参品名{}", ivd.getItName());
            return null;
        }
        return itIcMapping.get(ivd.getItName());
    }

    @Override
    public Date getVerifyDate() {
         return ivd.getAddedTime();
    }

    @Override
    public Date getAccountDate() {
         return ivd.getAccountDate();
    }

}
